package com.rapid.maintenance.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.rapid.maintenance.R;
import com.rapid.maintenance.ui.fragment.ActivityFragment;
import com.rapid.maintenance.ui.fragment.HomePageFragment;
import com.rapid.maintenance.ui.fragment.MineFragment;
import com.rapid.maintenance.ui.fragment.OrderFragment;
import com.rapid.maintenance.ui.fragment.PublishFragment;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * wy首页fragment
 */

public class MainFragmentAdapter extends FragmentPagerAdapter {

    private Map<String, Fragment> mFragmentEntry;
    private String[] mTitles = {"首页", "订单", "发布", "活动", "我的"};
    private WeakReference<Context> contextWeakReference;

    public MainFragmentAdapter(FragmentManager fm, Context context) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mFragmentEntry = new ConcurrentHashMap<>();
        contextWeakReference = new WeakReference<>(context);
        initFragments();

    }

    private void initFragments() {
        mFragmentEntry.put(mTitles[0], HomePageFragment.newInstance("", ""));
        mFragmentEntry.put(mTitles[1], OrderFragment.newInstance("", ""));
        mFragmentEntry.put(mTitles[2], PublishFragment.newInstance("", ""));
        mFragmentEntry.put(mTitles[3], ActivityFragment.newInstance("", ""));
        mFragmentEntry.put(mTitles[4], MineFragment.newInstance("", ""));
    }

    @Override
    @NonNull
    public Fragment getItem(int position) {
        return Objects.requireNonNull(mFragmentEntry.get(mTitles[position]));
    }

    @Override
    public int getCount() {
        return mTitles.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    }

    public View getTabView(int position) {
        View v = LayoutInflater.from(contextWeakReference.get()).inflate(R.layout.tab_custom, null);
        TextView tv = v.findViewById(R.id.tab_label);
        tv.setText(getPageTitle(position));
        ImageView img = v.findViewById(R.id.tab_icon);
        switch (position) {
            case 0:
                img.setBackgroundResource(R.drawable.home_unselect);
                break;
            case 1:
                img.setBackgroundResource(R.drawable.order_unselect);
                break;
            case 2:
                img.setBackgroundResource(R.drawable.publish_select);
                break;
            case 3:
                img.setBackgroundResource(R.drawable.activity_unselect);
                break;
            case 4:
                img.setBackgroundResource(R.drawable.mine_unselect);
                break;
        }
        v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return v;
    }
}